﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FUSE
{
    class CreationScreen : GameScene/*Microsoft.Xna.Framework.DrawableGameComponent*/
    {
        KeyboardState oldKeyboardState;
        public int ScreenSelection;
        //which of the ships the player has selected
        public int ShipSelection;
        int shipSelHead;
        int shipSelTail;
        bool doneOnce = true;

        Boolean hasScaled = false;
        //Sprites
        Sprite background;
        
        List<Texture2D> sShip;
        List<Texture2D> bShip;
        //Sprite speedShip;
        //Sprite normalShip;

        Sprite sLaunchPad;
        Sprite sShipPicThree;
        Sprite sShipPicOne;
        Sprite sShipPicTwo;

        Sprite launchButton;
        Sprite upArrow;
        Sprite downArrow;

        Vector2 unselectedShip = new Vector2(1.9f, 1.9f);
        Vector2 selectedShip = new Vector2((3f), (3f));
        Vector2 bigShipSelected = new Vector2(1.5f, 1.5f);

        float timeCheck = 0;

        //textures
        Texture2D[] tShips = new Texture2D[3];
        //Texture2D armourShipTex;
        //Texture2D speedShipTex;
        //Texture2D normalShipTex;



        SpriteBatch sb;
        GraphicsDeviceManager graphics;

        public CreationScreen(Game game) : base(game)
        {
            graphics = this.Game.Services.GetService(typeof(GraphicsDeviceManager)) as GraphicsDeviceManager;
            sb = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            this.Visible = false;
            this.Enabled = false;
        }

        public void LoadContent(ContentManager contentManager)
        {
            background.LoadContent(contentManager, "creationMenuBG");
            //ScaleBackground(background);

            ShipFile sf = (this.Game.Services.GetService(typeof(ShipFile)) as ShipFile);

            foreach (ShipData current in sf.loadedShips)
            {
                Texture2D temp = contentManager.Load<Texture2D>(current.iconTexture);

                sShip.Add(temp);
            }

            foreach (ShipData current in sf.loadedShips)
            {
                Texture2D temp = contentManager.Load<Texture2D>(current.bigTexture);

                bShip.Add(temp);
            }


            //sShip.LoadContent(contentManager, "Armor");
            //speedShip.LoadContent(contentManager, "SpeedShip");
            //normalShip.LoadContent(contentManager, "NormalShipRough");
            sShipPicThree.LoadContent(contentManager, "speedShipIcon");
            sShipPicOne.LoadContent(contentManager, "speedShipIcon");
            sShipPicTwo.LoadContent(contentManager, "speedShipIcon");
            sLaunchPad.LoadContent(contentManager,"speedShipBig");

            launchButton.LoadContent(contentManager, "launchButtonTex");
            upArrow.LoadContent(contentManager, "upArrow");
            downArrow.LoadContent(contentManager, "downArrow");
            

            //load textures
            //armourShipTex = contentManager.Load<Texture2D>("Armor");
            //normalShipTex = contentManager.Load<Texture2D>("NormalShipRough");
            //speedShipTex = contentManager.Load<Texture2D>("SpeedShip");
            //tShips[0] = contentManager.Load<Texture2D>("Armor");
            //tShips[1] = contentManager.Load<Texture2D>("NormalShipRough");
            //tShips[2] = contentManager.Load<Texture2D>("SpeedShip");

            //sShipPicOne.SetScale(new Vector2(.8f, .8f));
            //sShipPicTwo.SetScale(new Vector2(.8f, .8f));
            //SelectedShipPic.SetScale(new Vector2(.8f, .8f));

            

        }

        public void Initialize()
        {
            //sb = new SpriteBatch(graphics.GraphicsDevice);
            sShip = new List<Texture2D>();
            bShip = new List<Texture2D>();

            ScreenSelection = MyConstants.ICreationScreen;

            background = new Sprite(graphics);

            

            //sShip = new Sprite(graphics);
            //speedShip = new Sprite(graphics);
            //normalShip = new Sprite(graphics);
            sShipPicThree = new Sprite(graphics);
            sShipPicOne = new Sprite(graphics);
            sShipPicTwo = new Sprite(graphics);
            sLaunchPad = new Sprite(graphics);

            launchButton = new Sprite(graphics);
            upArrow = new Sprite(graphics);
            downArrow = new Sprite(graphics);

            background.SetX(0);
            background.SetY(0);

            //set default ship to first ship in the list
            ShipSelection = MyConstants.FirstShip;
            
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            /*if ((Game.Services.GetService(typeof(MapGenerator)) as MapGenerator).mapList.Count >=
                (Game.Services.GetService(typeof(MapGenerator)) as MapGenerator).numberOfMaps)
            {*/
                HandleButtonControls();

                //Draw();

                oldKeyboardState = Keyboard.GetState();
                if (hasScaled == false)
                {
                    ScaleMenuShips();
                    hasScaled = true;
                }
            //}
            base.Update(gameTime);
        }

        public override void Draw( GameTime gameTime)
        {
            /*if ((Game.Services.GetService(typeof(MapGenerator)) as MapGenerator).mapList.Count > 0)
            {*/
                //first line may be inefficient
                if (doneOnce)
                {

                    ScaleBackground(background);
                    doneOnce = false;
                }


                SetButtonLocation();

                background.Draw(sb);

                DrawMenuButtons();
            /*}
            else
            {
                SpriteBatch spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

                spriteBatch.Begin();

                String BUTTON_FONT = "scoreFont";
                ContentManager cm = Game.Content;
                SpriteFont sf = cm.Load<SpriteFont>(BUTTON_FONT);

                timeCheck += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if ((timeCheck > 0.5 && timeCheck < 1))
                {
                    spriteBatch.DrawString(sf, "Loading", new Vector2(200, 200), Color.Azure);
                }
                if (timeCheck >= 1 && timeCheck < 1.5)
                {
                    spriteBatch.DrawString(sf, "Loading.", new Vector2(200, 200), Color.Azure);
                }
                if (timeCheck >= 1.5 && timeCheck < 2)
                {
                    spriteBatch.DrawString(sf, "Loading..", new Vector2(200, 200), Color.Azure);
                }
                if (timeCheck >= 2 && timeCheck < 2.5)
                {
                    spriteBatch.DrawString(sf, "Loading...", new Vector2(200, 200), Color.Azure);
                    timeCheck = 0;
                }
                spriteBatch.End();
            }*/

            
        }

        /*
        private void ScaleBackground(Sprite backgroundSprite)
        {
            Vector2 tempScale;
             
            tempScale.Y = ((float)this.graphics.PreferredBackBufferHeight / (float)backgroundSprite.Size.Height);
            tempScale.X = ((float)this.graphics.PreferredBackBufferWidth / (float)backgroundSprite.Size.Width);
           
            backgroundSprite.SetScale(tempScale);
        }
        **/

        private void ScaleBackground(Sprite backgroundSprite)
        {
            Vector2 tempScale;
            //float heightRatio;
            //float widthRatio; 
            //tempScale.Y = ((float)this.graphics.PreferredBackBufferHeight / (float)backgroundSprite.Size.Height);
            //tempScale.X = ((float)this.graphics.PreferredBackBufferWidth / (float)backgroundSprite.Size.Width);
            tempScale.Y = ((float)backgroundSprite.Size.Height / (float)this.graphics.PreferredBackBufferHeight);
            tempScale.X = ((float)backgroundSprite.Size.Width / (float)this.graphics.PreferredBackBufferWidth);
            //float widthRatio = (float)(800f / 600f);
            graphics.PreferredBackBufferHeight = (int)((float)graphics.PreferredBackBufferHeight * tempScale.Y);
            graphics.PreferredBackBufferWidth = (int)((float)graphics.PreferredBackBufferWidth * tempScale.X);
            graphics.ApplyChanges();
            //backgroundSprite.SetScale(tempScale);
            //backgroundSprite.SetScale((heightRatio + widthRatio) / 2.0f);
        }

        

        public void SetButtonLocation()
        {


            //TODO calcuate button offset for each button position
            int buttonXOffset = (this.graphics.PreferredBackBufferWidth * 1 / 20);
            int buttonYOffset = (this.graphics.PreferredBackBufferHeight * 1 / 20);

            //set up arrow button location
            upArrow.SetX((int)(Math.Sqrt(
                Math.Pow(((upArrow.Size.Width / 2) - (upArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            upArrow.SetY((int)(Math.Sqrt(
                Math.Pow(((upArrow.Size.Height / 2) - (upArrow.Size.Height / 2)), 2)) + (buttonYOffset)+125));

            //set down arrow button location
            downArrow.SetX((int)(Math.Sqrt(
                Math.Pow(((downArrow.Size.Width / 2) - (downArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            downArrow.SetY((int)(Math.Sqrt(
                Math.Pow(((downArrow.Size.Height / 2) - (downArrow.Size.Height / 2)), 2)) + (this.graphics.PreferredBackBufferHeight - downArrow.Size.Height - buttonYOffset)));

            //set launch button location
            launchButton.SetX((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Width / 2) - (launchButton.Size.Width / 2)), 2)) + (this.graphics.PreferredBackBufferWidth - launchButton.Size.Width - buttonXOffset)));
            launchButton.SetY((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Height / 2) - (launchButton.Size.Height / 2)), 2)) + (buttonYOffset)));

            sShipPicThree.SetX((int)(Math.Sqrt(
               Math.Pow(((downArrow.Size.Width / 2) - (downArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            sShipPicThree.SetY((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Height / 2) - (launchButton.Size.Height / 2)), 2) + 60) + (this.graphics.PreferredBackBufferHeight/2 - sShipPicThree.Size.Height/2)));
            
            sShipPicOne.SetX((int)(Math.Sqrt(
               Math.Pow(((downArrow.Size.Width / 2) - (downArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            sShipPicOne.SetY((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Height / 2) - (launchButton.Size.Height / 2)), 2) + 60) + 
                (this.graphics.PreferredBackBufferHeight / 2 - sShipPicThree.Size.Height / 2) +
                (100) ));

            sShipPicTwo.SetX((int)(Math.Sqrt(
               Math.Pow(((downArrow.Size.Width / 2) - (downArrow.Size.Width / 2)), 2)) + (buttonXOffset)));
            sShipPicTwo.SetY((int)(Math.Sqrt(
                Math.Pow(((launchButton.Size.Height / 2) - (launchButton.Size.Height / 2) + 60), 2)) + 
                (this.graphics.PreferredBackBufferHeight / 2 - sShipPicThree.Size.Height / 2) -
                (100) ));


            sLaunchPad.SetX(375);
            sLaunchPad.SetY(225);

        }

        private void ScaleMenuShips()
        {
            sShipPicOne.SetScale(unselectedShip);

            sShipPicTwo.SetScale(unselectedShip);
            
            sShipPicThree.SetScale(selectedShip);

            sLaunchPad.SetScale(bigShipSelected);
        }

        private void DrawMenuButtons()
        {
            upArrow.Draw(sb);
            downArrow.Draw(sb);
            launchButton.Draw(sb);


           

            if (ShipSelection - 1 < MyConstants.FirstShip)
            {
                shipSelHead = sShip.Count -1;
            }
            else
            {
                shipSelHead = ShipSelection - 1;
            }

            if (ShipSelection + 2 > sShip.Count)
            {
                shipSelTail = 0;
            }
            else
            {
                shipSelTail = ShipSelection + 1;
            }

            if (ShipSelection < 0)
            {
                ShipSelection = sShip.Count - 1;
            }
            if (ShipSelection >= sShip.Count)
            {
                ShipSelection = 0;
            }

            //sShipPicThree.SetScale
            Vector2 tempScale1;
            Vector2 tempScale2;
            //Vector2 tempScale;
            //int oldWidth;
            //int oldHeight;
            //sShipPicThree.Size.X = 150;
            //sShipPicThree.Size.Y = 150;
            //tempScale2.Y = ((float)sShip.ElementAt(ShipSelection).Bounds.Height / (float)sShipPicThree.Size.Height);
            //tempScale2.X = ((float)sShip.ElementAt(ShipSelection).Bounds.Width / (float)sShipPicThree.Size.Width);  
            //tempScale1.Y = ((float)sShipPicThree.Size.Height / (float)sShip.ElementAt(ShipSelection).Bounds.Height);
            //tempScale1.X = ((float)sShipPicThree.Size.Width / (float)sShip.ElementAt(ShipSelection).Bounds.Width);
            //tempScale.Y = ((float)sShipPicThree.Size.Height / 150);
            //tempScale.X = ((float)sShipPicThree.Size.Width / 150);
                //sShipPicThree.SetScale(tempScale2);
            //oldWidth = sShipPicThree.Size.X;
            //oldHeight = sShipPicThree.Size.Y;
            //sShipPicThree.SetScale(tempScale);
                //sShipPicThree.SetScale(tempScale1);
            

            //Texture2D temp = sShip.ElementAt(ShipSelection);
            //temp.Bounds =

            sShipPicThree.SetTexture(sShip.ElementAt( ShipSelection));
            sShipPicOne.SetTexture(sShip.ElementAt(shipSelHead));
            sShipPicTwo.SetTexture(sShip.ElementAt(shipSelTail));

            //sShipPicThree.LoadContent(contentManager, sShip.ElementAt(ShipSelection).Name);


            


            /*
            //sShipPicThree.SetTexture(tShips[ShipSelection]);
            sShipPicThree.SetTexture(sShip.ElementAt(ShipSelection));

            if (ShipSelection - 1 < 0)
            {
                //sShipPicOne.SetTexture(tShips[MyConstants.LastShip]);
                sShipPicOne.SetTexture(sShip.Last());
=======

                sShipPicOne.SetTexture(tShips[MyConstants.LastShip]);
>>>>>>> .r446
                //sShipPicOne.SetColor(Color.Transparent);
            }
            else
            {
                //sShipPicOne.SetTexture(tShips[ShipSelection -1]);
                sShipPicOne.SetTexture(sShip.ElementAt(ShipSelection - 1));
                //sShipPicOne.SetColor(Color.White);
            }

            if (ShipSelection + 1 > sShip.Count)
            {
                //sShipPicTwo.SetTexture(tShips[MyConstants.FirstShip]);
                sShipPicTwo.SetTexture(sShip.First());
                //sShipPicTwo.SetColor(Color.Transparent);
            }
            else
            {
                //sShipPicTwo.SetTexture(tShips[ShipSelection + 1]);
                sShipPicTwo.SetTexture(sShip.ElementAt(ShipSelection + 1));
                //sShipPicTwo.SetColor(Color.White);
            }

            
            if (ShipSelection == MyConstants.ArmoredShip)
            {
                SelectedShipPic.SetTexture(armourShipTex);
            }
            else if (ShipSelection == MyConstants.NormalShip)
            {
                SelectedShipPic.SetTexture(normalShipTex);
            }
            else if (ShipSelection == MyConstants.SpeedShip)
            {
                SelectedShipPic.SetTexture(speedShipTex);
            }

             **/
            sLaunchPad.SetTexture(bShip.ElementAt(ShipSelection));

            sLaunchPad.Draw(sb);


            sShipPicOne.Draw(sb);

            sShipPicTwo.Draw(sb);
            sShipPicThree.Draw(sb);
        }

        //increments or decrements the menu selection based on key press
        private void HandleButtonControls()
        {

            if ((Keyboard.GetState().IsKeyUp(Keys.Up) && oldKeyboardState.IsKeyDown(Keys.Up)))
            {
                upArrow.SetColor(Color.Red);
                ShipSelection++;

                //decrements player selection
                if (ShipSelection < sShip.Count - 1)
                {
                    //ShipSelection++;
                }
                else
                {
                    //ShipSelection = 0;
                }
            }
            else
            {
                upArrow.SetColor(Color.White);
            }

            if ((Keyboard.GetState().IsKeyUp(Keys.Down) && oldKeyboardState.IsKeyDown(Keys.Down)))
            {
                downArrow.SetColor(Color.Red);

                ShipSelection--;

                // 3 is the last item
                if (ShipSelection > 0)
                {
                    //ShipSelection--;
                }
                else
                {
                    //ShipSelection = sShip.Count -1;
                }

            }
            else
            {
                downArrow.SetColor(Color.White);
            }

            if ((Keyboard.GetState().IsKeyUp(Keys.Escape) && oldKeyboardState.IsKeyDown(Keys.Escape)))
            {
                ScreenSelection = MyConstants.ITitleScreen;
            }

            if ((Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)) ||
                (Keyboard.GetState().IsKeyUp(Keys.Space) && oldKeyboardState.IsKeyDown(Keys.Space)))
            {
                //ShipFile sf = new ShipFile();
                //sf.LoadShipFile("c:/school/cmpt306/ships.xml");

                //launch
                //will normally be set to game screen
                //ScreenSelection = MyConstants.GalaxyScreen;
                //this.Enabled = false;
                //this.Visible = false;
                launchButton.SetColor(Color.Red);
                this.Hide();
                foreach (Galaxy item in Game.Components.OfType<Galaxy>())
                {
                    item.Show();
                }
            }
            else
            {
                launchButton.SetColor(Color.White);
            }

            if ((Keyboard.GetState().IsKeyUp(Keys.Escape) && oldKeyboardState.IsKeyDown(Keys.Escape)))
            {
                //this.Enabled = false;
                //this.Visible = false;
                //this.ScreenSelection = MyConstants.ITitleScreen;
                this.Hide();
                foreach (TitleScreen item in Game.Components.OfType<TitleScreen>())
                {
                    item.Show();
                }

                //(this.Game.Services.GetService(typeof(TitleScreen)) as TitleScreen).;

                doneOnce = true;
            }
            
        }

        public override void Hide()
        {
            this.Enabled = false;
            this.Visible = false;

            base.Hide();
        }

        public override void Show()
        {
            doneOnce = true;
            hasScaled = false;

            this.Enabled = true;
            this.Visible = true;

            base.Show();
        }
    }
}
